package com.sufen.gb.controller;

import com.alibaba.acm.shaded.org.codehaus.jackson.map.JsonMappingException;
import com.sufen.common.reponse.CodeMsg;
import com.sufen.common.reponse.CommonEntity;
import com.sufen.common.reponse.ResultMessage;
import com.sufen.common.request.BaseController;
import com.sufen.common.request.RequestParms;
import com.sufen.gb.service.Gb2HzyService;
import oracle.jdbc.proxy.annotation.Post;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.stereotype.Controller;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletResponse;
import java.sql.SQLSyntaxErrorException;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;


/**
 * @description:
 * @function:
 * @author: Liu Menglei
 * @Date: 2021/12/3 0003 22:16
 */
@Controller
@RequestMapping("/gb2Hzy")
public class Gb2HzyController extends BaseController {

    @Autowired
    Gb2HzyService gb2HzyService;

    private static final Logger logger = LoggerFactory.getLogger(Gb2HzyController.class);
    @PostMapping("/getPayItemsByCon")
    public void getPayItemsByCon(@RequestBody RequestParms requestParms, HttpServletResponse response){

        try {
            logger.info("根据条件拉取NCC应付单信息："+requestParms.toString()+"长度："+requestParms.getConditions().get("invoiceNos").toString().length());
            //请求条件信息校验
            if (CollectionUtils.isEmpty((Collection<?>) requestParms.getConditions().get("invoiceNos"))){
                logger.info("查询参数为空不能够进行有效查询");
                renderErrorJson(response,ResultMessage.error((long) CodeMsg.CONDITION_EMPTY.getCode(),
                        CodeMsg.CONDITION_EMPTY.getMsg()));return;
            }
            if (requestParms.getConditions().get("invoiceNos").toString().length()==2){
                logger.info("查询参数条件为空不能够进行有效查询");
                renderErrorJson(response,ResultMessage.error((long) CodeMsg.CON_EMPTY.getCode(),
                        CodeMsg.CON_EMPTY.getMsg()));
                return;
            }
            String str = requestParms.getConditions().get("invoiceNos").toString();
            String regex=".*[a-zA-Z]+.*";
            Matcher m= Pattern.compile(regex).matcher(str);
            boolean matches = m.matches();
            if (matches){
                logger.info("参数条件中包含无效英文字母不能够进行有效查询");
                renderErrorJson(response,ResultMessage.error((long) CodeMsg.ENGLISH_ERROR.getCode(),
                        CodeMsg.ENGLISH_ERROR.getMsg()));
                return;
            }
            List<CommonEntity> commonEntityList = gb2HzyService.getPayItemsByCon((List<String>)
                    requestParms.getConditions().get("invoiceNos"));
            logger.info("Result:"+commonEntityList.toString());
            renderJson(response, ResultMessage.data(commonEntityList));

        }catch (Exception e){
            if (e instanceof DataIntegrityViolationException){
                renderErrorJson(response,ResultMessage.error((long) CodeMsg.NUMBER_ERROR.getCode(),
                        CodeMsg.NUMBER_ERROR.getMsg()));
            }else {
                logger.info(e.getMessage());
                renderErrorJson(response,ResultMessage.error((long) CodeMsg.SERVER_ERROR.getCode(),
                        CodeMsg.SERVER_ERROR.getMsg()));
            }
        }
    }

    @PostMapping("/getPayItemsByDates")
    public void getPayItemsByDates(@RequestBody RequestParms requestParms, HttpServletResponse response){
        logger.info("根据条件获取NCC发票清算信息："+requestParms.toString());
        //请求条件信息校验
        if (CollectionUtils.isEmpty((Collection<?>) requestParms.getDatas().get("clearInvoices"))){
            logger.info("查询参数为空不能够进行有效查询");
            renderErrorJson(response,ResultMessage.error((long) CodeMsg.CONDITION_EMPTY.getCode(),
                    CodeMsg.CONDITION_EMPTY.getMsg()));return;
        }

        int apPayableitem =gb2HzyService.getPayItemsByDates((List<HashMap<String,Object>>) requestParms.getDatas().get("clearInvoices"));
        logger.info("Result:"+apPayableitem);
        renderJson(response,ResultMessage.data(apPayableitem));
    }
}